package net.gyapp.markim.module.contact;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.entity.MultiItemEntity;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.easyder.wrapper.base.adapter.ViewHelper;
import com.easyder.wrapper.base.listener.OnViewHelper;
import com.easyder.wrapper.base.presenter.MvpBasePresenter;
import com.easyder.wrapper.base.view.WrapperStatusFragment;
import com.easyder.wrapper.core.model.BaseVo;
import com.gyf.barlibrary.ImmersionBar;

import net.gyapp.markim.R;
import net.gyapp.markim.core.reponse.LoginResponse;
import net.gyapp.markim.module.contact.adapter.ContactAdapter;
import net.gyapp.markim.module.message.ChatActivity;
import net.gyapp.markim.widget.SlideBar;

import butterknife.BindView;


/**
 * Auther:  winds
 * Data:    2018/2/27
 * Version: 1.0
 * Desc:    首页
 */

@Deprecated
public class ContactFragment extends WrapperStatusFragment<MvpBasePresenter> {

    @BindView(R.id.mRecyclerView)
    RecyclerView mRecyclerView;
    @BindView(R.id.mSlideBar)
    SlideBar mSlideBar;
   @BindView(R.id.tv_title)
   TextView tv_title;
    @BindView(R.id.status_bar_view)
    View statusBarView;

    ContactAdapter adapter;

    public static ContactFragment newInstance(LoginResponse response) {
        ContactFragment fragment = new ContactFragment();
        Bundle bundle = new Bundle();
        bundle.putSerializable("response", response);
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    public int getViewLayout() {
        return R.layout.fragment_contact;
    }

    @Override
    public void onSupportVisible() {
        super.onSupportVisible();
        ImmersionBar.with(this).init();
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        ImmersionBar.with(this).statusBarView(statusBarView).init();
//        super.initView(savedInstanceState);
        adapter = new ContactAdapter();
        mRecyclerView.setLayoutManager(new LinearLayoutManager(_mActivity));
        mRecyclerView.setAdapter(adapter);
        adapter.addHeaderView(getInfoView());
        if (getArguments() != null && getArguments().getSerializable("response") != null) {
            adapter.setContactData(((LoginResponse) getArguments().getSerializable("response")).data.user);
        }
        mSlideBar.setOnTouchingLetterChangedListener(new SlideBar.OnTouchingLetterChangedListener() {
            @Override
            public void onTouchingLetterChanged(String s) {
                showToast(s);
            }
        });

        mRecyclerView.addOnItemTouchListener(new OnItemClickListener() {
            @Override
            public void onSimpleItemClick(BaseQuickAdapter baseQuickAdapter, View view, int position) {
                MultiItemEntity item = adapter.getItem(position);
                if (item instanceof LoginResponse.DataBean.UserBean.GroupsBean) {
                    LoginResponse.DataBean.UserBean.GroupsBean group = (LoginResponse.DataBean.UserBean.GroupsBean) item;
                    startActivity(ChatActivity.getIntent(_mActivity, group.group_id, group.name));
                } else if (item instanceof LoginResponse.DataBean.UserBean.UserChildBean) {
                    LoginResponse.DataBean.UserBean.UserChildBean user= (LoginResponse.DataBean.UserBean.UserChildBean) item;
                    startActivity(ChatActivity.getIntent(_mActivity, user.id, user.nick, user.avatar));
                }

            }
        });
    }

    @Override
    public void onLazyInitView(@Nullable Bundle savedInstanceState) {
        super.onLazyInitView(savedInstanceState);

    }

    @Override
    protected void loadData(Bundle savedInstanceState) {

    }

    @Override
    public void showContentView(String url, BaseVo dataVo) {

    }

    @Override
    protected boolean isLazyLoad() {
        return true;
    }


    private View getInfoView() {
        return getHelperView(mRecyclerView, R.layout.header_contact, new OnViewHelper() {
            @Override
            public void help(ViewHelper helper) {

            }
        });
    }
}
